package com.mm.service.impl;

import com.mm.bo.*;
import com.mm.exception.CustomException;
import com.mm.mapper.*;
import com.mm.org.n3r.idworker.Sid;
import com.mm.pojo.*;
import com.mm.redis.RedisService;
import com.mm.rediskey.AdminKey;
import com.mm.service.DepartmentInfoService;
import com.mm.util.PageUtil;
import com.mm.vo.DepastaffVo;
import com.mm.vo.EmpPunishListVo;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 林友航
 * @version 1.0
 * @date 2020/12/4 10:59
 */
@Service
public class DepartmentInfoServiceImpl implements DepartmentInfoService {

    @Resource
    private DepartmentInfoMapper departmentInfoMapper;
    @Resource
    private Sid sid;

    @Resource
    private RedisService redisService;
    @Resource
    private LogsInfoMapper logsInfoMapper;

    @Resource
    private AssessEvaluateInfoMapper assessEvaluateInfoMapper;

    @Resource
    private EmployeeInfoMapper employeeInfoMapper;
    @Resource
    private EmpPunishInfoMapper empPunishInfoMapper;

    /**
     * 部门管理筛选(工号、姓名、入职时间、部门)
     *
     * @param departmentSearchBo
     * @return
     */
    @Override
    public PageUtil<DepastaffVo> selectDepartmentInfo(DepartmentSearchBo departmentSearchBo) {
        //筛选信息总条数
        Integer count = departmentInfoMapper.findDepartmentListCount(departmentSearchBo);
        //筛选信息列表
        List<DepastaffVo> departmentStaffList = departmentInfoMapper.findDepartmentList(departmentSearchBo);
        PageUtil<DepastaffVo> pageUtil = new PageUtil<>(count, count, departmentSearchBo.getPageIndex(), departmentSearchBo.getPageSize(), departmentStaffList);
        return pageUtil;
    }

    @Override
    public Integer updateDepartmentInfo(DepartmentSearchBo departmentSearchBo) {
        return null;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insertDepaInfo(InserDepaInfoBo inserDepaInfoBo) throws Exception {
        //获取当前管理员的信息
        AdministratorInfo administratorInfoRedis = redisService.get(AdminKey.admin, inserDepaInfoBo.getToken(), AdministratorInfo.class);
        DepartmentInfo departmentInfo = new DepartmentInfo();
        BeanUtils.copyProperties(departmentInfo, inserDepaInfoBo);
        departmentInfo.setDepaId(sid.nextShort());
        int row = departmentInfoMapper.insertSelective(departmentInfo);
        //添加日志信息
        //获取当前时间戳
        long timeMillis = System.currentTimeMillis();
        //获取日志详情
        String log_detail = "管理员：" + administratorInfoRedis.getAdminRealName() + "添加的部门：" + inserDepaInfoBo.getDepaName();
        LogsInfo logsInfo = new LogsInfo(sid.nextShort(), administratorInfoRedis.getAdminId(), log_detail, timeMillis, timeMillis);
        logsInfoMapper.insertSelective(logsInfo);
        return row;
    }

    @Override
    public List<DepartmentInfo> queryDepaList() {
        Example example = new Example(DepartmentInfo.class);
        example.createCriteria().andEqualTo("depaStatus", 1);

        List<DepartmentInfo> departmentInfos = departmentInfoMapper.selectByExample(example);
        return departmentInfos;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer updateDepaInfo(DepaUpdateBo depaUpdateBo) throws Exception {
        //获取当前管理员的信息
        AdministratorInfo administratorInfoRedis = redisService.get(AdminKey.admin, depaUpdateBo.getToken(), AdministratorInfo.class);
        DepartmentInfo departmentInfo = new DepartmentInfo();
        BeanUtils.copyProperties(departmentInfo, depaUpdateBo);
        int rowDepa = departmentInfoMapper.updateByPrimaryKey(departmentInfo);
        //添加日志信息
        //获取当前部门信息
        DepartmentInfo departmentInfoDB = departmentInfoMapper.selectByPrimaryKey(depaUpdateBo.getDepaId());
        //获取当前时间戳
        long timeMillis = System.currentTimeMillis();
        //获取日志详情
        String log_detail = "管理员：" + administratorInfoRedis.getAdminRealName() + "修改的部门：" + departmentInfoDB.getDepaName() + "为：" + depaUpdateBo.getDepaName();
        LogsInfo logsInfo = new LogsInfo(sid.nextShort(), administratorInfoRedis.getAdminId(), log_detail, timeMillis, timeMillis);
        logsInfoMapper.insertSelective(logsInfo);
        return rowDepa;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer deleteDepaInfo(String depaId, String token) {
        //获取当前管理员的信息
        AdministratorInfo administratorInfoRedis = redisService.get(AdminKey.admin, token, AdministratorInfo.class);
        //获取当前部门信息
        DepartmentInfo departmentInfoDB = departmentInfoMapper.selectByPrimaryKey(depaId);
        departmentInfoDB.setDepaStatus(0);
        int rowDepa = departmentInfoMapper.updateByPrimaryKeySelective(departmentInfoDB);
        //获取当前时间戳
        long timeMillis = System.currentTimeMillis();
        //获取日志详情
        String log_detail = "管理员：" + administratorInfoRedis.getAdminRealName() + "删除了部门：" + departmentInfoDB.getDepaName();
        LogsInfo logsInfo = new LogsInfo(sid.nextShort(), administratorInfoRedis.getAdminId(), log_detail, timeMillis, timeMillis);
        logsInfoMapper.insertSelective(logsInfo);
        return rowDepa;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer evaluationEmpInfo(EvaluationEmpInfoBo evaluationEmpInfoBo) throws Exception {
        //获取当前管理员的信息
        AdministratorInfo administratorInfoRedis = redisService.get(AdminKey.admin, evaluationEmpInfoBo.getToken(), AdministratorInfo.class);
        //获取被添加考核评价的员工信息
        EmployeeInfo employeeInfoDB = employeeInfoMapper.selectByPrimaryKey(evaluationEmpInfoBo.getEmpId());
        if (employeeInfoDB == null) {
            throw new CustomException("不存在当前员工信息");
        }
        AssessEvaluateInfo assessEvaluateInfo = new AssessEvaluateInfo();
        BeanUtils.copyProperties(assessEvaluateInfo, evaluationEmpInfoBo);
        //评价Id
        assessEvaluateInfo.setAsseId(sid.nextShort());
        //管理员Id
        assessEvaluateInfo.setAdminId(administratorInfoRedis.getAdminId());
        //获取当前时间戳
        long timeMillis = System.currentTimeMillis();
        assessEvaluateInfo.setAsseCreateTime(timeMillis);
        assessEvaluateInfo.setAsseUpdateTime(timeMillis);
        int rowAss = assessEvaluateInfoMapper.insertSelective(assessEvaluateInfo);
        //获取日志详情
        String log_detail = "管理员：" + administratorInfoRedis.getAdminRealName() + "添加了员工：" + employeeInfoDB.getEmpName() + "的考核评价";
        LogsInfo logsInfo = new LogsInfo(sid.nextShort(), administratorInfoRedis.getAdminId(), log_detail, timeMillis, timeMillis);
        logsInfoMapper.insertSelective(logsInfo);
        return rowAss;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer punishEmpInfo(PunishEmpInfoBo punishEmpInfoBo) throws Exception {
        //获取当前管理员的信息
        AdministratorInfo administratorInfoRedis = redisService.get(AdminKey.admin, punishEmpInfoBo.getToken(), AdministratorInfo.class);
        //获取被添加考核评价的员工信息
        EmployeeInfo employeeInfoDB = employeeInfoMapper.selectByPrimaryKey(punishEmpInfoBo.getEmpId());
        if (employeeInfoDB == null) {
            throw new CustomException("不存在当前员工信息");
        }
        EmpPunishInfo empPunishInfo = new EmpPunishInfo();
        BeanUtils.copyProperties(empPunishInfo, punishEmpInfoBo);
        //处罚Id
        empPunishInfo.setPuniId(sid.nextShort());
        //处罚人
        empPunishInfo.setAdminId(administratorInfoRedis.getAdminId());
        //获取当前时间戳
        long timeMillis = System.currentTimeMillis();
        //处罚时间
        empPunishInfo.setPuniTime(timeMillis);
        //创建时间
        empPunishInfo.setPuniCreateTime(timeMillis);
        //修改时间
        empPunishInfo.setPuniUpdateTime(timeMillis);
        int rowEmp = empPunishInfoMapper.insertSelective(empPunishInfo);

        //获取日志详情
        String log_detail = "管理员：" + administratorInfoRedis.getAdminRealName() + "添加了员工：" + employeeInfoDB.getEmpName() + "的处罚信息";
        LogsInfo logsInfo = new LogsInfo(sid.nextShort(), administratorInfoRedis.getAdminId(), log_detail, timeMillis, timeMillis);
        logsInfoMapper.insertSelective(logsInfo);
        return rowEmp;
    }

    @Override
    public PageUtil<EmpPunishListVo> queryEmpPunishList(EmpPunishListBo empPunishListBo) {
        //查询总数
        Integer count = empPunishInfoMapper.queryEmpPunishListCount(empPunishListBo);
        //查询列表
        List<EmpPunishListVo> empPunishListVos = empPunishInfoMapper.queryEmpPunishList(empPunishListBo);
        PageUtil<EmpPunishListVo> page = new PageUtil<>(count, count, empPunishListBo.getPageIndex(), empPunishListBo.getPageSize(), empPunishListVos);
        return page;
    }
}

